//
//  StatusViewModel.swift
//  WB
//
//  Created by leador on 17/5/10.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    
    //MARK: ---定义属性---
    var status:Status?
    
    var cellHeight:CGFloat = 0
    
    //MARK: ---对数据处理的属性---
    var sourceText:String?
    
    var dateText:String? {
        
        get {
            
            if let created_at = self.status?.created_at {
                
                return NSDate.createDateString(createDateStr: created_at)
                
            }else {
                
                return nil
                
            }
        }
        
    }
    
    var vipImage:UIImage?
    
    var verifiedImage:UIImage?
    
    var profileUrl:URL?
    
    var picURLs:[URL] = [URL]()
    
    init(status:Status) {
        
        self.status = status
        
        if let source = status.source, status.source != "" {
            
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location-startIndex
            
            sourceText = (source as NSString).substring(with: NSRange(location:startIndex,length:length))
            
        }
        
        //        if let created_at = status.created_at {
        //            dateText = NSDate.createDateString(createDateStr: created_at)
        //        }
        
        
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            verifiedImage = #imageLiteral(resourceName: "avatar_vip") //个人认证
        case 2,3,5:
            verifiedImage = #imageLiteral(resourceName: "avatar_enterprise_vip") //企业认证
        case 220:
            verifiedImage = #imageLiteral(resourceName: "avatar_grassroot") //微博达人
        default:
            verifiedImage = nil
        }
        
        
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named:"common_icon_membership_level\(mbrank)")
        }
        
        let profileStr = status.user?.profile_image_url ?? ""
        profileUrl = URL(string:profileStr)
        
        let picURlDicts = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        if let picURLDicts = picURlDicts {
            
            for picURLDict in picURLDicts {
                
                guard let picURLString = picURLDict["thumbnail_pic"] else {
                    continue
                }
                
                picURLs.append(URL(string:picURLString)!)
                
            }
            
        }
        
    }
    
}
